<?php

namespace app\modules\v1\controllers;

use Yii;
use yii\rest\ActiveController;
use yii\web\NotFoundHttpException;
use app\modules\v1\models\Log;

/**
 * json通用遵守
 * {
 *      "id":"",
 *      "telephone":"",
 *      "username":"",
 *      "age":"",
 *      "sexy":"",
 *      "address":"",
 *      "email":"",
 *      "money":"",
 * }
 *
 {
    "telephone": "13129711111",
    "username": "某用户",
    "age": 18,
    "sexy": "女",
    "address": "北京...",
    "email": "123@qq.com",
    "token":"MjAxNy0wMy0wNw==8a72f5a9e00f51bc11a53c5aa871c7c6"
}
 */
class UserInfoController extends ActiveController
{
    public $modelClass = 'app\modules\v1\models\Userdetail';
    
    public function actions(){
        
        $actions = parent::actions();
        
        unset($actions['index'],$actions['create'],$actions['delete'],$actions['view'],$actions['update']);
        
    }
    
    public function actionIndex($telephone){
        
        $this->log('GET',json_encode(Yii::$app->request->get(),JSON_UNESCAPED_UNICODE));
        
        $modelClass = $this->modelClass;
        
        $model = $modelClass::find()->where(['telephone' => $telephone])->one();
        
        return $model;
        
    }
    
    public function actionCreate(){
        /*
        $this->log('POST', Yii::$app->request->getRawBody());
        
        $model = new $this->modelClass;
        
        $data = json_decode(Yii::$app->request->getRawBody(),true);
        
        $model->attributes = $data;
        
        $this->tokenValidate($data['token']);
        
        if(!$model->save()){
            return $model->getFirstErrors();
        }
        
        return $model;*/
    }
    
    public function actionUpdate($id){
        
        $this->log('PUT', Yii::$app->request->getRawBody());

        $model = $this->findModel($id);
        
        $data = json_decode(Yii::$app->request->getRawBody(),true);
        
        $model->attributes = $data;
        
        $this->tokenValidate($data['token']);
        
        if(!$model->save()){
            return $model->getFirstErrors();
        }
        
        return $model;
        
    }
    
    public function actionDelete($id){
        /*
        $this->log('DELETE', json_encode(['ID' => $id]));
        
        $model = $this->findModel($id);
        
        $model->delete();
        */
    }
    
    public function actionView($id){
        
        $this->log('GET', json_encode(['ID' => $id]));
        
        $model = $this->findModel($id);

        return $model;
        
    }
    
    protected function findModel($id){
        
        $modelClass = $this->modelClass;
        
        $model = $modelClass::findOne($id);
        
        if($model != null){
            return $model;
        }else{
            throw new NotFoundHttpException('404');
        }
        
    }
    
    protected function log($op,$data){
        
        $log = new Log();
        
        $log->attributes = ['operation' => $op,'class' => 'UserInfo','requestData' => $data,'dateTime' => date('Y-m-d H:i:s'),'ip' => Yii::$app->request->userIP];
        
        if(!$log->save()){
            return $log->getFirstError();
        }
        
    }

    public function tokenValidate($token){
        if($token != Yii::$app->params['token']){
            throw new NotFoundHttpException;
        }else{
            return true;
        }
    }
    
}
